package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddneedlistBinding;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedListItemEntity;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.adapter.enterprise.AddNeedListServiceAdpater;
import com.sgb.kjwl.view.ui.adapter.enterprise.AddNeedListTrandeAdpater;
import com.sgb.kjwl.view.widget.ClearEditText;
import com.sgb.kjwl.viewmodel.enterprise.AddNeedListModel;
import com.sgb.link.view.widget.CustomXTabLayout;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddNeedListActivity extends AppActivity {

    private ActivityAddneedlistBinding mBinding;
    private AddNeedListTrandeAdpater adpater_trande;
    private AddNeedListModel mModel;
    Map<String, Object> mMap = new HashMap<>();

    @Override
    protected void initView() {
        super.initView();
        mModel = new AddNeedListModel(getApplication());
        mModel.index = getIntent().getIntExtra("index", 0);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_addneedlist);
        mBinding.setVariable(BR.mNeedListModel, mModel);
        mBinding.setLifecycleOwner(this);

        initRecyclerView();
        initObserve();
        initTablayout();
        initlister();


    }

    private void initRecyclerView() {
        adpater_trande = new AddNeedListTrandeAdpater(this, null);
        mBinding.recyclerViewTrand.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerViewTrand.setAdapter(adpater_trande);
    }

    private void initObserve() {
        mModel.list_trande.observe(this, new Observer<List<NeedListItemEntity>>() {
            @Override
            public void onChanged(List<NeedListItemEntity> objects) {


                if (mModel.pagenum_trand > 1) {
                    mBinding.recyclerViewTrand.loadMoreComplete();
                    adpater_trande.addDatas(objects);
                    if (objects.size() == 0) {
                        mBinding.recyclerViewTrand.setNoMore(true);
                    }
                } else {
                    adpater_trande.setDatas(objects);

                    if (adpater_trande.getDatas().size() == 0) {
                        mBinding.emptyView.setVisibility(View.VISIBLE);
                        mBinding.include.setVisibility(View.VISIBLE);
                        ((TextView) mBinding.include.findViewById(R.id.tv_no_data)).setText(!TextUtils.isEmpty(mModel.find_data.getValue()) ? mModel.isfind_trande ? "暂无数据" : "暂无数据" : "暂无数据");
                        ((TextView) mBinding.include.findViewById(R.id.no_data_count)).setVisibility(TextUtils.isEmpty(mModel.find_data.getValue()) ? View.GONE : View.VISIBLE);

                    } else {
                        if (objects.size() == 20) {
                            mBinding.recyclerViewTrand.setLoadingMoreEnabled(true);
                        }
                        mBinding.emptyView.setVisibility(View.GONE);
                    }
                    mBinding.recyclerViewTrand.smoothScrollToPosition(0);
                    mBinding.recyclerViewTrand.refreshComplete();
                }
            }
        });


        mModel.updateValue.observe(this, new Observer<NeedListItemEntity>() {
            @Override
            public void onChanged(NeedListItemEntity needListItemEntity) {
                mModel.getList_service(true);
            }
        });

        mModel.show_loading.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer) {
                    case 1:
                        com.swgk.core.dialog.DialogHelper.showProgressDialog(AddNeedListActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                        break;
                    case 2:
                        com.swgk.core.dialog.DialogHelper.dismissProgressDialog();
                        break;

                }
            }
        });
    }

    private void initTablayout() {
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("全部"));
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("报价中"));
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("已结束"));

    }


    private void initlister() {

        mBinding.ivBack.setOnClickListener(v -> finish());
        mBinding.rightViewPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.rlAddlayout.setVisibility(View.VISIBLE);
            }
        });

        mBinding.rightViewSearch.setOnClickListener((view) -> {


            mMap.put("title", mBinding.cetSearch.getText().toString());
            mModel.getList_Trande(true, mMap);

        });

        mBinding.imageColose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.rlAddlayout.setVisibility(View.GONE);
            }
        });

        mBinding.llNeedSb.setOnClickListener((view) -> {
            AddNeed_TrandActivity.startactivity(this);
            mBinding.rlAddlayout.setVisibility(View.GONE);
        });
        mBinding.llNeedZl.setOnClickListener((view) -> {
            AddNeed_PatentActivity.startactivity(this);
            mBinding.rlAddlayout.setVisibility(View.GONE);
        });

        mBinding.llNeedBq.setOnClickListener((view) -> {
            AddNeed_CopyRightActivity.startactivity(this);
            mBinding.rlAddlayout.setVisibility(View.GONE);
        });

        mBinding.llNeedGs.setOnClickListener((view) -> {
            AddNeed_CompanyActivity.startactivity(this);
            mBinding.rlAddlayout.setVisibility(View.GONE);
        });

        mBinding.llNeedZc.setOnClickListener((view) -> {
            AddNeed_AssetsActivity.startactivity(this);
            mBinding.rlAddlayout.setVisibility(View.GONE);
        });

        mBinding.llNeedService.setOnClickListener((view) -> {
            AddNeed_ServiceActivity.startactivity(this);
            mBinding.rlAddlayout.setVisibility(View.GONE);
        });

        mBinding.cetSearch.OnClickClear(new ClearEditText.OnClickClear() {
            @Override
            public void clickClear() {
                mMap.remove("title");
                mModel.getList_Trande(true, mMap);
            }
        });
        mBinding.rlAddlayout.setOnTouchListener((View, MotionEvent) -> {
            return true;
        });
        mBinding.recyclerViewTrand.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mModel.getList_Trande(true, mMap);
            }

            @Override
            public void onLoadMore() {
                mModel.getList_Trande(false, mMap);
            }
        });
        mBinding.tabLayout.addOnTabSelectedListener(new CustomXTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                if (tab.getText().equals("全部")) {
                    mMap.remove("demandStatus");
                } else if (tab.getText().equals("报价中")) {
                    mMap.put("demandStatus", 1);
                } else if (tab.getText().equals("已结束")) {
                    mMap.put("demandStatus", 2);
                }
                mModel.getList_Trande(true, mMap);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        adpater_trande.setOnclickLister(lister);
    }

    AddNeedListServiceAdpater.ListonclickLister lister = new AddNeedListServiceAdpater.ListonclickLister() {
        @Override
        public void click(View v, int position) {
            switch (v.getId()){
                case R.id.btn_check_baojia:
                    isNeedRefreshTab = false;
                    NeedSignUpListActivity.start(AddNeedListActivity.this,adpater_trande.getDatas().get(position).getId() + "");
                    break;
                default:
                    PublishNeedDataDateilActivity.startactivity(AddNeedListActivity.this, adpater_trande.getDatas().get(position).getId() + "");
                    break;
            }


        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mModel.getList_Trande(true, mMap);
        if (isNeedRefreshTab && mBinding.tabLayout.getTabCount() > 0) {
            mBinding.tabLayout.getTabAt(0).select();
        }
    }

    private static boolean isNeedRefreshTab = false;

    public static void startactivity(Context mContext) {
        isNeedRefreshTab = true;
        Intent mIntent = new Intent(mContext, AddNeedListActivity.class);
        mContext.startActivity(mIntent);
    }

}
